package cn.edu.zzuli.admin.controller;

import cn.edu.zzuli.admin.service.ComplaintMessageService;
import cn.edu.zzuli.admin.service.UserService;
import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.common.utils.NowDate;
import cn.edu.zzuli.common.utils.WeChatUtil;
import cn.edu.zzuli.model.dto.admin.ComDto;
import cn.edu.zzuli.model.dto.admin.ComplaintDto;
import cn.edu.zzuli.model.entity.ComplaintInfo;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.model.vo.wx.WxMessage;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/background/admin/complaint")
public class ComplaintMessageController {

    @Autowired
    private ComplaintMessageService messageService;

    @Autowired
    private UserService userService;

    @ApiOperation("根据分页参数以及选择的条件查询列表")
    @GetMapping("list/{page}/{limit}")
    public ResultInfo list(@PathVariable Integer page,
                           @PathVariable Integer limit){
        Page<ComplaintDto> complaintDtoList = messageService.findAll(page,limit);
        return Result.ok(complaintDtoList);
    }

    @ApiOperation("处理申诉/投诉")
    @GetMapping("dealWith/{id}")
    public ResultInfo dealWith(@PathVariable Integer id){
        ComDto comDto = messageService.findComDtoById(id);
        System.out.println(comDto);
        return Result.ok(comDto);
    }

    @ApiOperation("更新完成状态")
    @GetMapping("update/{id}/{sta}")
    public ResultInfo update(@PathVariable Integer id,@PathVariable Integer sta){
        ComplaintInfo complaintInfo = new ComplaintInfo();
        complaintInfo.setId(id);
        complaintInfo.setDealed(sta);
        boolean res = messageService.updateById(complaintInfo);
        return res == true ? Result.ok() :  Result.fail();
    }


    @ApiOperation("处理回复")
    @GetMapping("advice/{message}/{id}")
    public ResultInfo advice(@PathVariable String message,
                           @PathVariable Integer id){
        User user = userService.getById(id);
        System.out.println(user);
        JSONObject json = new JSONObject();
        json.set("time1", new JSONObject().set("value", NowDate.getNowTime()));
        json.set("thing2", new JSONObject().set("value", message));
        WeChatUtil.wxSend(WxMessage.USER_CHAT,user.getOpenId(),json);
        return Result.ok();
    }

}
